﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace YJY_DMZS.BasisFile
{
    public partial class FrmEmployee : XtraForm
    {
        public FrmEmployee()
        {
            InitializeComponent();
            InitGrid();
        }

        private void FrmEmployee_Load(object sender, EventArgs e)
        {
            LoadDepartment();
            LoadEmployee();
        }
        /// <summary>
        /// 加载部门
        /// </summary>
        private void LoadDepartment()
        {
            var bll = new DMZS.BLL.DepartmentNew();
            var ds = bll.GetListForUIExpand(string.Empty);
            tlsDepartment.DataSource = ds.Tables[0];
            tlsDepartment.ExpandAll();
        }
        /// <summary>
        /// 加载员工
        /// </summary>
        private void LoadEmployee()
        {
            int DeptID;
            if (tlsDepartment.FocusedNode != null)
            {
                DeptID = Convert.ToInt32(tlsDepartment.FocusedNode["ID"]);
            }
            else
            {
                return;
            }
            var bll = new DMZS.BLL.Employee();
            grdEmployee.DataSource = bll.GetListForUIExpand(DeptID, string.Empty).Tables[0];
        }

        /// <summary>
        /// 加载员工
        /// </summary>
        /// <param name="DeptID"></param>
        private void LoadEmployee(int DeptID)
        {
            if (tlsDepartment.FocusedNode != null)
            {
                DeptID = Convert.ToInt32(tlsDepartment.FocusedNode["ID"]);
            }
            else
            {
                return;
            }
            var bll = new DMZS.BLL.Employee();
            grdEmployee.DataSource = bll.GetListForUIExpand(DeptID, string.Empty).Tables[0];
        }
        private void btnNew_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            var frm = new FrmEmployee_Editor();
            frm.ID = 0;
            frm.ShowDialog();
        }
        private void btnModify_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (grvEmployee.GetFocusedDataRow() == null)
            {
                return;
            }
            var frm = new FrmEmployee_Editor();
            frm.ID = Convert.ToInt64(grvEmployee.GetFocusedDataRow()["ID"]);
            frm.ShowDialog();
        }

        private void tlsDepartment_FocusedNodeChanged(object sender, DevExpress.XtraTreeList.FocusedNodeChangedEventArgs e)
        {
            if (tlsDepartment.FocusedNode == null)
            {
                return;
            }
            LoadEmployee();
        }

        private void btnDelete_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (grvEmployee.GetFocusedDataRow() == null)
            {
                return;
            }
            if (DialogResult.Yes == XtraMessageBox.Show("确认删除?", Text, MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1))
            {
                var bll = new DMZS.BLL.Employee();
                bll.Delete(Convert.ToInt64(grvEmployee.GetFocusedDataRow()["ID"]));

                LoadEmployee();
            }
        }

        private void btnRefresh_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            LoadDepartment();
            LoadEmployee();
        }

        private void grvEmployee_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
        {
            CommonControl.DxGridHelper.DrawRowIndicator(sender, e);
        }

        /// <summary>
        /// 初始网格显示
        /// </summary>
        private void InitGrid()
        {
            grvEmployee.IndicatorWidth = grvEmployee.IndicatorWidth == -1 ? 40 : grvEmployee.IndicatorWidth;
            grvEmployee.OptionsView.ShowIndicator = true;

            CommonControl.DxGridHelper.InitGridStyle(grvEmployee);
            CommonControl.DxGridHelper.InitGridStyle(tlsDepartment);
        }

        private void grvEmployee_DoubleClick(object sender, EventArgs e)
        {
            if (grvEmployee.DataSource == null || grvEmployee.RowCount == 0)
            {
                return;
            }
            var frm = new FrmEmployee_Editor();
            frm.ID = Convert.ToInt32(grvEmployee.GetFocusedDataRow()["ID"]);
            frm.ShowDialog();
        }
    }
}
